package com.xmkj.winmoney.my;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.common.Entity.AddressEntity;
import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.AddressListMethods;
import com.common.retrofit.method.DeleteAddressMethods;
import com.common.retrofit.method.SetDefaultAddressMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.adapter.CommonAdapter;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.winmoney.R;
import com.xmkj.winmoney.my.adapter.AdressAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Create by leo
 * 创建日期：2018/5/10
 * 描述：
 */

public class AddressListActivity extends BaseMvpActivity {
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    @BindView(R.id.tv_add_newaddress)
    TextView tvAddNewaddress;
    private AdressAdapter adapter;
    private List<AddressEntity.ListsBean> bean = new ArrayList<>();
    private AddressEntity.ListsBean infobean;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_list;
    }

    @Override
    protected void onViewCreated() {
        attachClickListener(tvAddNewaddress);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==tvAddNewaddress.getId()){//添加地址
            Intent intent = new Intent(context,AddressModifyActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void doLogicFunc() {

        gotoHttp();
        setRecyclerView();
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.ADD_ADDRESS) && (Boolean) rxKeyEvent.getValue()) {
                    mPageIndex = 1 ;
                    gotoHttp();

                }
            }
        }));
    }
    private void setRecyclerView() {

        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);
        adapter = new AdressAdapter(context, bean);
        recyclerview.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new CommonAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(CommonAdapter adapter, View view, int position) {
                infobean = (AddressEntity.ListsBean) adapter.mData.get(position);
                if (view.getId() == R.id.iv_setdefault) { //删除
                    gotoSetDefault(infobean.addressid);
                } else if (view.getId() == R.id.iv_compile||view.getId()==R.id.tv_compile) { //编辑
                    Intent intent = new Intent(context,AddressModifyActivity.class);
                    intent.putExtra(AddressModifyActivity.FROM,infobean);
                    startActivity(intent);
                } else if (view.getId() == R.id.tv_delete||view.getId()==R.id.iv_delete) { //删除
                    deleteAddress(infobean.addressid,position);
                }
            }
        });

        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttp();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttp();
            }
        });
    }

    private void gotoSetDefault(int addressid) {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                showToastMsg("设置成功");
                mPageIndex = 1 ;
                gotoHttp();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        SetDefaultAddressMethods.getInstance().setDefault(subscriber, uid, hashid, addressid );
        rxManager.add(subscriber);
    }

    private void deleteAddress(int addressid , final int position) {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                adapter.remove(position);
                showToastMsg("删除成功");

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        DeleteAddressMethods.getInstance().delAddress(subscriber, uid, hashid, addressid);
        rxManager.add(subscriber);
    }

    private void gotoHttp() {
        CommonSubscriber<AddressEntity> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                AddressEntity beans = (AddressEntity) o;
                List<AddressEntity.ListsBean> beanList = beans.lists;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        AddressListMethods.getInstance().userAddress(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), mPageIndex);
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("收货地址");
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
